package com.woniuxy.meal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.meal.common.RedisKey;
import com.woniuxy.meal.entity.FoodMonthSale;
import com.woniuxy.meal.dao.FoodMonthSaleMapper;
import com.woniuxy.meal.entity.FoodVariety;
import com.woniuxy.meal.entity.StoreInfo;
import com.woniuxy.meal.service.FoodMonthSaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.meal.service.FoodVarietyService;
import com.woniuxy.meal.service.StoreInfoService;
import com.woniuxy.meal.service.dto.StoreInfoForEsDto;
import com.woniuxy.meal.util.DateUtils;
import com.woniuxy.meal.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜品月销量表 服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月12日
 */
@Service
public class FoodMonthSaleServiceImpl extends ServiceImpl<FoodMonthSaleMapper, FoodMonthSale> implements FoodMonthSaleService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private FoodVarietyService foodVarietyService;
    @Autowired
    private StoreInfoService storeInfoService;
    @Autowired
    private ElasticsearchRestTemplate esTemplate;



    //更新商铺总销售量，每月月底更新一次
    @Override
    public void updateStoreSale() throws Exception {
        String storeSaleKey = RedisKey.getStoreSaleKey();
        LocalDate localDate = LocalDate.now();
        String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy年MM月"));

        QueryWrapper<FoodMonthSale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dateStr",dateStr);
        List<FoodMonthSale> list = list(queryWrapper);
        for (FoodMonthSale foodMonthSale : list) {
            redisUtil.zremove(storeSaleKey,foodMonthSale.getStoreId().toString());
            redisUtil.zadd(storeSaleKey,foodMonthSale.getStoreId().toString(),foodMonthSale.getCount());
        }

    }

    //更新每种菜品的销售量，每天凌晨更新
    @Override
    public void updateFoodSale() throws Exception {
        List<FoodMonthSale> list = new ArrayList<>();
        String foodSaleKey = RedisKey.getFoodSaleKey();
        Set<String> ids = redisUtil.zrange(foodSaleKey);
        List<FoodVariety> foodVarieties = foodVarietyService.listByIds(ids);
//        System.out.println(foodVarieties);
        for (FoodVariety foodVariety : foodVarieties) {
            FoodMonthSale foodMonthSale = new FoodMonthSale();
            foodMonthSale.setStoreId(foodVariety.getStoreId());
            foodMonthSale.setFoodId(foodVariety.getId());
            double count = redisUtil.zscore(foodSaleKey, foodVariety.getId().toString());
            foodMonthSale.setCount(Double.valueOf(count).intValue());
            LocalDate localDate = LocalDate.now();
            String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy年MM月"));

            foodMonthSale.setDateStr(dateStr);
            foodMonthSale.setFinalDate(LocalDateTime.now());
            list.add(foodMonthSale);
        }
        for (FoodMonthSale foodMonthSale : list) {
            UpdateWrapper<FoodMonthSale> wrapper = new UpdateWrapper<>();
            wrapper.eq("foodId",foodMonthSale.getFoodId());
            LocalDate localDate = LocalDate.now();
            String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy年MM月"));
            wrapper.eq("dateStr",dateStr);
            saveOrUpdate(foodMonthSale,wrapper);
        }

    }

    @Override
    public void updateStoreSaleES() throws Exception {
        List<StoreInfo> list = storeInfoService.list();
        String key = RedisKey.getStoreSaleKey();
        for (StoreInfo storeInfo : list) {
            StoreInfoForEsDto storeInfoForEsDto = BeanUtil.toBean(storeInfo, StoreInfoForEsDto.class);
            int zscore = (int)redisUtil.zscore(key, storeInfo.getId().toString());
            storeInfoForEsDto.setCount(zscore);
            esTemplate.save(storeInfoForEsDto);
        }
    }
}
